package com.jyw.business.de.service.impl;

import java.util.List;

import com.jyw.business.de.constant.DemandProcessConstant;
import com.jyw.business.de.constant.DemandStateConstant;
import com.jyw.business.de.domain.DeDemandInfo;
import com.jyw.business.de.domain.dto.web.DemandAuditPassDto;
import com.jyw.business.de.domain.dto.web.DemandOperDto;
import com.jyw.business.de.domain.dto.web.DemandProjectAuditPageDto;
import com.jyw.business.de.domain.vo.web.DemandProjectAuditPageVO;
import com.jyw.business.de.domain.vo.web.DemandProjectAuditVO;
import com.jyw.business.de.service.IDeDemandInfoService;
import com.jyw.business.de.service.IDeDemandProcessService;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import com.jyw.business.de.mapper.DeDemandProjectAuditMapper;
import com.jyw.business.de.domain.DeDemandProjectAudit;
import com.jyw.business.de.service.IDeDemandProjectAuditService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 需求-需求立项审核Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@Service
public class DeDemandProjectAuditServiceImpl extends ServiceImpl<DeDemandProjectAuditMapper,DeDemandProjectAudit> implements IDeDemandProjectAuditService
{
    @Resource
    private DeDemandProjectAuditMapper deDemandProjectAuditMapper;

    @Resource
    private IDeDemandInfoService deDemandInfoService;

    @Resource
    private IDeDemandProcessService processService;

    /**
     * 查询需求-需求立项审核
     * 
     * @param id 需求-需求立项审核主键
     * @return 需求-需求立项审核
     */
    @Override
    public DeDemandProjectAudit selectDeDemandProjectAuditById(Long id)
    {
        return deDemandProjectAuditMapper.selectDeDemandProjectAuditById(id);
    }

    /**
     * 查询需求-需求立项审核列表
     * 
     * @param deDemandProjectAudit 需求-需求立项审核
     * @return 需求-需求立项审核
     */
    @Override
    public List<DeDemandProjectAudit> selectDeDemandProjectAuditList(DeDemandProjectAudit deDemandProjectAudit)
    {
        return deDemandProjectAuditMapper.selectDeDemandProjectAuditList(deDemandProjectAudit);
    }

    /**
     * 新增需求-需求立项审核
     * 
     * @param deDemandProjectAudit 需求-需求立项审核
     * @return 结果
     */
    @Override
    public int insertDeDemandProjectAudit(DeDemandProjectAudit deDemandProjectAudit)
    {
        deDemandProjectAudit.setCreateTime(DateUtils.getNowDate());
        return deDemandProjectAuditMapper.insertDeDemandProjectAudit(deDemandProjectAudit);
    }

    /**
     * 修改需求-需求立项审核
     * 
     * @param deDemandProjectAudit 需求-需求立项审核
     * @return 结果
     */
    @Override
    public int updateDeDemandProjectAudit(DeDemandProjectAudit deDemandProjectAudit)
    {
        return deDemandProjectAuditMapper.updateDeDemandProjectAudit(deDemandProjectAudit);
    }

    /**
     * 批量删除需求-需求立项审核
     * 
     * @param ids 需要删除的需求-需求立项审核主键
     * @return 结果
     */
    @Override
    public int deleteDeDemandProjectAuditByIds(Long[] ids)
    {
        return deDemandProjectAuditMapper.deleteDeDemandProjectAuditByIds(ids);
    }

    /**
     * 删除需求-需求立项审核信息
     * 
     * @param id 需求-需求立项审核主键
     * @return 结果
     */
    @Override
    public int deleteDeDemandProjectAuditById(Long id)
    {
        return deDemandProjectAuditMapper.deleteDeDemandProjectAuditById(id);
    }

    @Override
    public List<DemandProjectAuditPageVO> auditPage(DemandProjectAuditPageDto dto) {
        return ConversionUtil.conversionList(deDemandProjectAuditMapper.auditPage(dto),DemandProjectAuditPageVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pass(DemandAuditPassDto dto) {
        //查询需求立项审核
        DeDemandProjectAudit audit = getById(dto.getId());
        if(ObjectUtils.isEmpty(audit)){
            throw new ServiceException("需求立项审核不存在");
        }
        if(!audit.getProjectState().equals("0")){
            throw new ServiceException("需求立项审核已经审批,请勿重复审批");
        }
        //设置项目名称和项目负责人
        lambdaUpdate()
                .eq(DeDemandProjectAudit::getId,dto.getId())
                .set(DeDemandProjectAudit::getProjectName,dto.getProjectName())
                .set(DeDemandProjectAudit::getProjectUser,dto.getProjectUser())
                .set(DeDemandProjectAudit::getAuditTime,DateUtils.getNowDate())
                .set(DeDemandProjectAudit::getAuditUser, SecurityUtils.getUserId())
                .set(DeDemandProjectAudit::getProjectState,"2")
                .update();
        //修改需求状态
        deDemandInfoService.lambdaUpdate()
                .eq(DeDemandInfo::getId,audit.getDemandId())
                .set(DeDemandInfo::getDemandState, DemandStateConstant.PROJECT)
                .update();
        //记录需求立项审核数据
        //todo 记录项目编号与项目id
        processService.saveProcess(audit.getDemandId(), DemandProcessConstant.PROJECT_AUDIT,new DemandProjectAuditVO(Long.valueOf(System.currentTimeMillis()).toString(),null));
    }

    @Override
    public void reject(DemandOperDto dto) {
        //查询需求立项审核
        DeDemandProjectAudit audit = getById(dto.getId());
        if(ObjectUtils.isEmpty(audit)){
            throw new ServiceException("需求立项审核不存在");
        }
        if(!audit.getProjectState().equals("0")){
            throw new ServiceException("需求立项审核已经审批,请勿重复审批");
        }
        //设置项目名称和项目负责人
        lambdaUpdate()
                .eq(DeDemandProjectAudit::getId,dto.getId())
                .set(DeDemandProjectAudit::getAuditTime,DateUtils.getNowDate())
                .set(DeDemandProjectAudit::getAuditUser, SecurityUtils.getUserId())
                .set(DeDemandProjectAudit::getProjectState,"1")
                .update();
        //修改需求状态
        deDemandInfoService.lambdaUpdate()
                .eq(DeDemandInfo::getId,audit.getDemandId())
                .set(DeDemandInfo::getDemandState, DemandStateConstant.PROCESSING)
                .update();
        //记录需求立项审核数据
        processService.saveProcess(audit.getDemandId(), DemandProcessConstant.PROJECT_AUDIT,new DemandProjectAuditVO(dto));
    }
}
